package maquinavotar.view;

import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import maquinavotar.MaquinaVotarApp;
import maquinavotar.dao.VoteDAOImplXml;

public class ConfirmVoteScreen extends JPanel {
    
    private static ConfirmVoteScreen INSTANCE = null;
    
    private ConfirmVoteScreen() {}
            
    private synchronized static void createInstance(final MaquinaVotarApp app) {
        
        INSTANCE = new ConfirmVoteScreen();
        INSTANCE.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Confirmación de voto"));
        INSTANCE.setLayout(new GridBagLayout());
        
        JButton btn_confirm = new JButton("Confirmar", new ImageIcon("imgs/ok.png"));
        btn_confirm.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    JOptionPane.showMessageDialog(null, "Voto realizado exitosamente. Puede retirar su tarjeta.", "Confirmación de voto", JOptionPane.INFORMATION_MESSAGE);

                    VoteDAOImplXml v = new VoteDAOImplXml();
                    v.saveVoto(app.vote);
                    
                    app.vote = null;
                    
                    app.showScreen(0,2);
                }
            });
        INSTANCE.add(btn_confirm);
        
        JButton btn_edit = new JButton("Editar", new ImageIcon("imgs/edit.png"));
        btn_edit.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    app.showScreen(1,2);
                }
            });
        INSTANCE.add(btn_edit);
    }
    
    public static ConfirmVoteScreen getInstance(final MaquinaVotarApp app) {
        if (INSTANCE == null)
            createInstance(app);
        return INSTANCE;
    }

}